// pages/taxes/taxes.js
import {
  stopStay,
  getRecordList,
  delRecord,
  getStay,
} from '../../api/daysOfStay/daysOfStay'
import { _t } from '../../i18n/base'
const util = require('../../utils/util.js')
Page({
  data: {
    reset: false,
    remind: false,
    entryStatus: 0,
    count: 0,
    entryData: {},
    entryDataList: [],
    lang: {},
    showDatePicker: false,
    currentDateStr: '',
    currentDate: new Date().getTime(),
    minDate: new Date().getFullYear() + '-01-01',
    maxDate: new Date().getTime(),
    showTimePicker: false,
    timeStr: '',
    columns: [],
    leaveStr: '',
    RecordListMap: {},
  },
  // 跳转主页
  onClickLeft() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  // 新增提示
  addTaxes() {
    wx.navigateTo({
      url: '/pages/addTaxes/addTaxes',
    })
  },
  // 跳转详情页
  toTaxExempDetailHandle() {
    wx.navigateTo({
      url: '/pages/taxExemptionPolicyDetail/taxExemptionPolicyDetail',
    })
  },
  // 删除按钮
  deleteHandle(e) {
    let id = e.currentTarget.dataset.recordid
    wx.showModal({
      cancelColor: '#F95F22',
      cancelText: this.data.lang['取消'],
      confirmColor: '#999999',
      confirmText: this.data.lang['确定'],
      content: this.data.lang['删除记录提醒'],
      showCancel: true,
      success: (result) => {
        if (result.confirm) {
          this.removeRecord(id)
        }
      },
    })
  },
  // 暂停重启按钮
  reset() {
    if (this.data.entryData.entryStatus) {
      // 暂停状态点击
      this.showOrHandle()
    } else {
      wx.navigateTo({
        url: `/pages/addTaxes/addTaxes?stayId=${this.data.entryData.stayId}`,
      })
    }
  },
  // 暂停离境
  async stopStayHandle() {
    let res = await stopStay({
      stayId: this.data.entryData.stayId,
      leaveStr: this.data.leaveStr,
    })
    if (res.code == 200) {
      this.isStatus()
    }
  },
  // 打开日期选择弹窗
  showOrHandle() {
    this.setData({
      showDatePicker: !this.data.showDatePicker,
    })
  },
  // 关闭日期选择弹窗
  showOrHidden() {
    this.setData({
      showDatePicker: !this.data.showDatePicker,
    })
  },
  // 关闭 时间弹窗
  onTimeCloseHandle() {
    this.setData({
      showTimePicker: false,
    })
  },
  // 确定日期
  confirmDate(e) {
    // if (detail == 'Invalid Date') {
    //   wx.showToast({
    //     title: '请选择日期',
    //   })
    // } else {
    //   this.setData({
    //     currentDate: detail,
    //     currentDateStr:e.detail.date,
    //     showDatePicker: false,
    //     showTimePicker: true,
    //   })
    // }
    this.setData({
      currentDate: e.detail.date,
      currentDateStr: e.detail.date,
      showDatePicker: false,
      showTimePicker: true,
    })
  },
  // 确认时分
  onTimeConfirmHandle({ detail }) {
    let val = detail.value
    let hours = val[0]
    let mins = val[1]
    // 暂停时间
    let time =
      new Date(this.data.currentDate).getTime() + hours * 60 * 60 + mins
    let createTime = this.data.entryData.entryTime
    let createSpecificDate = createTime.split(' ')[0]
    let createHAM = createTime.split(' ')[1]
    let createHours = createHAM.split(':')[0]
    let createMins = createHAM.split(':')[1]
    // 入境时间
    let entryTime =
      new Date(createSpecificDate).getTime() +
      createHours * 60 * 60 +
      createMins
    this.setData({
      leaveStr: `${this.data.currentDateStr} ${hours}:${mins}`,
      showTimePicker: false,
    })
    if (time <= entryTime) {
      wx.showToast({
        title: this.data.lang['暂停时间不得小于入境时间'],
        icon: 'none',
      })
    } else {
      this.stopStayHandle()
    }
  },
  // 取消时分
  onTimeCancelHandle() {
    this.setData({
      showTimePicker: false,
    })
  },
  // 获取出入境列表
  async getEntryExitList() {
    let { result } = await getRecordList(this.data.entryData)
    if (result) {
      this.setData({
        RecordListMap: result,
      })
      wx.setStorageSync('entryDataList', JSON.stringify(result))
    } else {
      this.setData({
        RecordListMap: [],
      })
    }
  },
  // 删除出入境记录
  async removeRecord(id) {
    let res = await delRecord(id)
    // 删除成功之后再次刷新列表
    if (res.code == 200) {
      this.isStatus()
    }
  },
  //获取当前状态
  async isStatus() {
    let { result } = await getStay({ userId: this.data.userId })
    if (result&&result.userId) {
      this.setData({
        entryData: result,
      })
      this.getEntryExitList()
    }
  },
  // 初始化时分
  eggTime() {
    let hours = []
    let mins = []
    for (let i = 0; i <= 23; i++) {
      let hour = i.toString().padStart(2, '0')
      hours.push(hour)
    }
    for (let j = 0; j <= 59; j++) {
      let min = j.toString().padStart(2, '0')
      mins.push(min)
    }
    let columns = [
      {
        values: hours,
        defaultIndex: 0,
      },
      {
        values: mins,
        defaultIndex: 0,
      },
    ]
    this.setData({
      columns,
    })
  },
  // 页面只有第一次进入才调用一次
  onLoad() {
    this.setData({
      lang: _t(),
      language: wx.getStorageSync('language'),
    })
    wx.setNavigationBarTitle({
      title: this.data.lang['停留日数'],
    })
  },
  // 初始化
  initLoginStatus() {
    let userId = wx.getStorageSync('userId')
    if (!userId) {
      wx.setStorageSync('loginShow', 1)
      wx.switchTab({
        url: '/pages/index/index',
      })
    } else {
      this.setData({
        userId,
      })
      this.isStatus()
    }
  },
  // 进入此页面即调用
  onShow() {
    this.eggTime()
    this.initLoginStatus()
  },
})
